/**
*@Author      ：anlzou
*@Date        ：18:00 2021/4/23
*@Github      ：https://github.com/anlzou
*@Description ：
*/
package com.anlzou.bank;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyncReentrantLockBank extends Bank{
    private int count = 0;// 账户余额

    //需要声明这个锁
    private Lock lock = new ReentrantLock();

    // 存钱
    public void addMoney(int money) {
        lock.lock();        //上锁
        try {
            count += money;
            System.out.println(System.currentTimeMillis() + "存进：" + money);
        }finally {
            lock.unlock();  //解锁
        }
    }

    // 取钱
    public void subMoney(int money) {
        lock.lock();        //上锁
        try {
            if (count - money < 0) {
                System.out.println("余额不足");
                return;
            }
            count -= money;
            System.out.println(+System.currentTimeMillis() + "取出：" + money);
        }finally {
            lock.unlock();  //解锁
        }
    }

    // 查询
    public void lookMoney() {
        System.out.println("账户余额：" + count);
    }
}
